package binary_search.leetcode_704;

public class BinarySearch {
    public int search(int[] nums, int target) {
        //保持区间左闭右闭原则[left...right]
        int left = 0;
        int right = nums.length - 1;
        //左闭右闭区间循环条件是 <=
        while(left <= right){
            int mid = left + (right - left) / 2;
            if(nums[mid] == target){
                return mid;
            }else if(nums[mid] > target){
                //左边元素都不符合规则，直接排除
                left = mid + 1;
            }else{
                //右边元素都不符合规则，直接排除
                right = mid - 1;
            }
        }
        //没找到，返回-1
        return -1;
    }
}
